package com.xuetang9.fujian.threaddemo;

import javax.swing.*;
import java.util.concurrent.TimeUnit;

/**
 * @author: fujian
 * @description: 用来更新进度条
 * @date: 2021/5/2 10:36
 * @version: V1.0
 * @package: com.xuetang9.fujian.threaddemo
 **/
public class ProgressTask implements Runnable{
    private JProgressBar progressBar;

    private boolean isRunning = true;

    public ProgressTask(JProgressBar progressBar) {
        this.progressBar = progressBar;
    }

    // 让任务结束
    public void stop(){
        this.isRunning = false;
    }

    @Override
    public void run() {
        while (progressBar.getValue() <= 100 && isRunning){
            progressBar.setValue(progressBar.getValue() + 1);
            try {
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
